const formatDate = (date, split="/") => {
    const year = date.getFullYear();
    const month = date.getMonth() + 1;
    const day = date.getDate();
  
    return (
      [year, month, day].map(formatNumber).join(split)
    );
  };

const formatTime = (date) => {
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hour = date.getHours();
  const minute = date.getMinutes();
  const second = date.getSeconds();

  return (
    [year, month, day].map(formatNumber).join("/") +
    " " +
    [hour, minute, second].map(formatNumber).join(":")
  );
};

const formatNumber = (n) => {
  n = n.toString();
  return n[1] ? n : "0" + n;
};

function textRichFormat(html = '') {
  return html.replace(
    /<img/gi,
    '<img style="max-width:100%;height:auto;display:block" '
  );
}

// 判断图片，视频
function fileType(file) {
  if (!file || typeof file != "string") {
    return false;
  }

  const suffix = file.substring(file.lastIndexOf(".") + 1).toLowerCase();
  if (["png", "gif", "jpg", "jpeg", "webp", "bpm"].indexOf(suffix) > -1) {
    return "image";
  }
  if (["avi", "mov", "rmvb", "rm", "flv", "mp4", "3gp"].indexOf(suffix) > -1) {
    return "video";
  }
  return suffix;
}

// 获取当月
function getMonthStr() {
  const now = new Date();

  return now.getFullYear() + "-" + formatNumber(now.getMonth() + 1);
}

// back prev page
function toPrevPage() {
  const pages = getCurrentPages();

  const prevPage = pages[pages.length - 2];

  prevPage.onLoad();

  wx.navigateBack({
    delta: 1,
  });
}

module.exports = {
  formatDate,
  formatTime: formatTime,
  textRichFormat,
  fileType,
  toPrevPage,
  getMonthStr,
};
